/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ASColor;
import org.aswing.JFrame;
import org.aswing.JPanel;

import com.kairos.utils.Monitor;
import com.kairos.utils.monitor.MonitorField;

import fever.utils.ArrayUtil;
import fever.utils.FMonitor;
import fever.utils.Stringifier;

/**
 * @author Romain Ecarnot
 */
class fvaswing.components.FvMonitorFrame extends JFrame
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : FvMonitorFrame;
	
	private var _mcContainer : MovieClip;
	private var _monitor : Monitor;
	private var _aBuffer : Array;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * {@code FvMonitorFrame} singleton access.
	 * 
	 * @param	width	{@code Number} of pixel of the frame's width.
	 * @param 	height	{@code Number} of pixel of the frame's height.
	 * 
	 * @return <strong>FvMonitorFrame</strong> instance
	 */
	public static function getInstance( width : Number, height : Number ) : FvMonitorFrame
	{
		if( !_instance ) _instance = new FvMonitorFrame( width, height );
		return _instance;
	}
	
	/**
	 * Constructor.
	 * 
	 * @param	width	{@code Number} of pixel of the frame's width.
	 * @param 	height	{@code Number} of pixel of the frame's height.
	 */ 
	public function FvMonitorFrame( width : Number, height : Number ) 
	{
		super( 'Kairos Monitor', false );
		
		if( isNaN( width ) || width <= 0 ) width = 250;
		if( isNaN( height ) || height <= 0 ) height = 200;
		
		setSize( width, height );
		setClosable( false );
		setResizable( false );
		setBackground( new ASColor( FMonitor.COLOR_BACKGROUND ) );
		
		_aBuffer = new Array();
		
		getContentPane().addEventListener( JFrame.ON_PAINT, _onCreated, this );
	}
	
	/**
	 * Adds the passed-in <code>com.kairos.utils.MonitorField</code> into the monitor.
	 * 
	 * @param  	o	<code>com.kairos.utils.MonitorField</code> to add into the monitor.	
	 */
	public function addField ( o : MonitorField ) : Void
	{
		if( isShowing() && _monitor )
		{
			_monitor.addField( o );
		}
		else _aBuffer.push( o );
	}
	
	/**
	 * Removes the passed-in {@code com.kairos.utils.MonitorField} from the monitor.
	 * 
	 * <p>Returns {@code true} if the field have been removed from
	 * the monitor, {@code false} otherwise.</p>
	 * 
	 * @param  	o	{@code com.kairos.utils.MonitorField} to remove from the monitor.	
	 * @return 	{@code true} if the field have been removed from
	 * 			the monitor, {@code false} otherwise.
	 */
	public function removeField ( o : MonitorField ) : Void
	{
		if( isShowing() && _monitor )
		{
			_monitor.removeField( o );
		}
		else ArrayUtil.remove( o, _aBuffer );
	}
	
	/**
	 * Returns embded <strong>Kairos monitor</strong>.
	 */
	public function getMonitor(  ) : Monitor
	{
		return _monitor;
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _onCreated( source : JPanel ) : Void
	{
		var l : Number = _aBuffer.length;
		
		var mc : MovieClip = getContentPane().createMovieClipOnRoot( 'tmp_' );
		var pmc : MovieClip = mc._parent;
		mc.removeMovieClip();
		
		_mcContainer = getContentPane().createMovieClip( '__monMC__' );
		_monitor = new Monitor( _mcContainer, pmc._width - 10, pmc._height - ( l * 20 ) );
		
		for ( var i : Number = 0; i < l; i++ )  _monitor.addField( _aBuffer[ i ] );
		
		_aBuffer = new Array();
		_monitor.start();
	}
}